package com.imooc.demo.service.impl;


import com.imooc.demo.dao.UserInfoDao;
import com.imooc.demo.entity.UserAddressinfo;
import com.imooc.demo.entity.UserInfo;
import com.imooc.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoDao userInfoDao;





/////////////////////////
   @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryOpenidExist(String buyerOpenid) {
        UserInfo userInfo= new UserInfo();
       userInfo.setBuyerOpenid(buyerOpenid);
       UserInfo result=userInfoDao.getUserInfoByOpenid1(buyerOpenid);
        return result==null?false:true;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUserInfo(UserInfo userInfo) {
       userInfoDao.insertUserInfo(userInfo);

    }

    @Override
    public boolean insertUserInfo(UserInfo userInfo) {
        return userInfoDao.insertUserInfo(userInfo);
    }

    @Override
    public boolean updateUserInfo(UserInfo userInfo) {
        return userInfoDao.updateUserInfo(userInfo);
    }

    @Override
    public List<UserAddressinfo> getUserInfoByOpenid(String buyerOpenid) {
        return userInfoDao.getUserInfoByOpenid2(buyerOpenid);
    }

    @Override
    public UserInfo getUserInfo(String buyerOpenid) {
        return userInfoDao.getUserInfo(buyerOpenid);
    }

    @Override
    public boolean insertPhone(UserInfo userInfo) {
        return userInfoDao.insertPhone(userInfo);
    }

    @Override
    public UserInfo getUserIndex(String buyerOpenid) {
        return userInfoDao.getUserInfoByOpenid1(buyerOpenid);
    }

   /* @Override
    public boolean updateUserAddress(UserAddressinfo userAddressinfo) {
        //空值判断，主要是category1Id不能为空
        if(userAddressinfo.getAddressId()>0&&userAddressinfo.getAddressBuyerPhone()!=null&&userAddressinfo.getAddressArea()!=null&&userAddressinfo.getAddressDetail()!=null) {
            try {
                //更新种类信息
                int effectedNum = userInfoDao.updateUserAddress(userAddressinfo);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("更新地址信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("更新地址信息失败：" + e.toString());
            }
        }else{
            throw new RuntimeException("地址信息不能为空！");
        }
    }

    @Override
    public boolean removeUserAddress(Integer addressId) {
        //空值判断，主要是category1Id不能为空
        if(addressId!=null) {
            try {
                //更新种类信息
                int effectedNum = userInfoDao.removeUserAddress( addressId);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("删除地址信息失败！");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除地址信息失败：" + e.toString());
            }
        }else{
            throw new RuntimeException("地址信息不能为空！");
        }
    }

    @Override
    public boolean insertUserAddressinfo(UserAddressinfo userAddressinfo) {
        if(userAddressinfo.getAddressUserName()!=null && !"".equals(userAddressinfo.getAddressUserName())&&
        userAddressinfo.getAddressBuyerPhone()!=null && !"".equals(userAddressinfo.getAddressBuyerPhone())&&
        userAddressinfo.getAddressArea()!=null && !"".equals(userAddressinfo.getAddressArea())&&
        userAddressinfo.getAddressDetail()!=null&&!"".equals(userAddressinfo.getAddressDetail())&&
        userAddressinfo.getBuyerOpenid()!=null && !"".equals(userAddressinfo.getBuyerOpenid())){
            try{
                int effectedNum = userInfoDao.insertUserAddressinfo(userAddressinfo);
                if(effectedNum>0){
                    return  true;
                }else{
                    throw  new RuntimeException("插入地址信息失败！");
                }
            }catch (Exception e){
                throw new RuntimeException("插入地址信息失败！");
            }
        }else{
            throw new RuntimeException("地址不能为空！");
        }
    }

    @Override
    public UserAddressinfo getAddressinfoById(Integer addressId) {
        return userInfoDao.getAddressinfoById(addressId);
    }*/

}

